package main;

import entity.Student;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

public class Main {
    private EinUndAusgabe io = new EinUndAusgabe();
    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("aufgabe10PU");
    private EntityManager em = emf.createEntityManager();
    
    public void dialog() {
        int eingabe = -1;
        while (eingabe != 0) {
            System.out.println(
                    "0 Beenden\n"
                    + "1 neuer Studi\n"
                    + "2 Studi bearbeiten\n"
                    + "3 alle Studis zeigen:  ");
            eingabe = io.leseInteger();
            switch (eingabe) {
                case 1: {
                    neuerStudi();
                    break;
                }
                case 2: {
                    studiBearbeiten();
                    break;
                }
                case 3: {
                    alleStudis();
                    break;
                }
            }
        }
        
        //Schliessen
        if(em != null && em.isOpen()) 
            em.close();
        if(emf != null && emf.isOpen())
            emf.close();

        
   
    }

    private void neuerStudi() {
        System.out.print("Matrikelnummer: ");
        int mat = io.leseInteger();
        System.out.print("Name: ");
        String name = io.leseString();

        //TODO
        em.getTransaction().begin();
        try {
            em.persist(new Student(mat, name));
            em.getTransaction().commit();
        } catch (Exception e) {
            if(em.getTransaction().isActive())
                em.getTransaction().rollback();
            throw e;
        }
       
        //TODO ENDE
        
    }

    private void studiBearbeiten() {
        System.out.print("Matrikelnummer: ");
        int mat = io.leseInteger();
        Student tmp = null;

        //TODO
        tmp = em.find(Student.class, mat);
        //TODO ENDE
        
        
        if (tmp == null) {
            System.out.println("gibbet nich");
            return;
        }
        System.out.print("Name: ");
        String name = io.leseString();
        
        //TODO
        em.getTransaction().begin();
        try {
            tmp.setName(name);
            em.getTransaction().commit();
        } catch(Exception e) {
            if(em.getTransaction().isActive())
                em.getTransaction().rollback();
            throw e;
        }
        
        //TODO ENDE

        
    }

    private void alleStudis() {
        //TODO
        for(Student s : em.createQuery("SELECT s FROM Student s", 
                Student.class).getResultList()) {
            System.out.println("Matrikel: " + s.getMatrikel()
                    + ", Name: " + s.getName());
        }
        
        //TODO ENDE

    }

    public static void main(String[] s) {
        new Main().dialog();
    }
}
